package cn.itrip.service.impl;

import cn.itrip.entity.ItripAreaDic;
import cn.itrip.dao.ItripAreaDicDao;
import cn.itrip.service.ItripAreaDicService;
import cn.itrip.vo.HotelCityVO;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 区域字典表(ItripAreaDic)表服务实现类
 *
 * @author makejava
 * @since 2022-04-14 22:34:54
 */
@Service("itripAreaDicService")
public class ItripAreaDicServiceImpl implements ItripAreaDicService {
	@Resource
	private ItripAreaDicDao itripAreaDicDao;

	@Override
	public List<HotelCityVO> queryHotelCity(Long isChina) {
		List<HotelCityVO> hotelCityVOS = new ArrayList<>();
		itripAreaDicDao.queryHotelCity(isChina).stream().forEach(a ->{
			hotelCityVOS.add(
					new HotelCityVO()
					.setId(a.getId())
					.setName(a.getName()));
		});
		return hotelCityVOS;
	}

	/**
	 * 通过ID查询单条数据
	 *
	 * @param id 主键
	 * @return 实例对象
	 */
	@Override
	public ItripAreaDic queryById(Long id) {
		return this.itripAreaDicDao.queryById(id);
	}

	/**
	 * 分页查询
	 *
	 * @param itripAreaDic 筛选条件
	 * @param pageRequest  分页对象
	 * @return 查询结果
	 */
	@Override
	public Page<ItripAreaDic> queryByPage(ItripAreaDic itripAreaDic, PageRequest pageRequest) {
		long total = this.itripAreaDicDao.count(itripAreaDic);
		return new PageImpl<>(this.itripAreaDicDao.queryAllByLimit(itripAreaDic, pageRequest), pageRequest, total);
	}

	/**
	 * 新增数据
	 *
	 * @param itripAreaDic 实例对象
	 * @return 实例对象
	 */
	@Override
	public ItripAreaDic insert(ItripAreaDic itripAreaDic) {
		this.itripAreaDicDao.insert(itripAreaDic);
		return itripAreaDic;
	}

	/**
	 * 修改数据
	 *
	 * @param itripAreaDic 实例对象
	 * @return 实例对象
	 */
	@Override
	public ItripAreaDic update(ItripAreaDic itripAreaDic) {
		this.itripAreaDicDao.update(itripAreaDic);
		return this.queryById(itripAreaDic.getId());
	}

	/**
	 * 通过主键删除数据
	 *
	 * @param id 主键
	 * @return 是否成功
	 */
	@Override
	public boolean deleteById(Long id) {
		return this.itripAreaDicDao.deleteById(id) > 0;
	}
}
